package ModelLayer;
import java.util.*;

/**
 * Write a description of class Copy here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Copy
{
    // instance variables - replace the example below with your own
    private int serialNumber;
    private String purchaseDate;
    private int price;
    private HashSet<Loan> loans;

    /**
     * Constructor for objects of class Copy
     */
    public Copy()
    {
        serialNumber=0;
        purchaseDate=" ";
        price=0;
        
    }

    
    public boolean checkAvailable()
    {
        Iterator<Loan> it=loans.iterator();
        while((it.hasNext()))
        {
            Loan loan=it.next();
            if(!loan.readState())
                return false;
        }
        return true;
    }
   
    public HashSet<Loan> readLoans()
    {
        return loans;
    }
    
    public int readSize()
    {
        return loans.size();
    }
    
   public void create(int serialNumber, String purchaseDate, int price)
   {
        this.serialNumber=serialNumber;  
        this.purchaseDate=purchaseDate;  
        this.price=price;
        loans=new HashSet<>();
   }
   
   public boolean createLoan(Person person, int id, String borrowDate, String period)
    {
        if(loans.size()!=0)
        {
           Iterator<Loan> it=loans.iterator();
        while(it.hasNext())
        {
            Loan existingLoan=it.next();
            if(existingLoan.readID()==id)
                return false;
            else
            {
                Loan loan=new Loan();
                loan.createLoan(id,borrowDate,period);
                loan.updateCopy(this);
                loan.updatePerson(person);
                loan.updateState(false);
                loans.add(loan);
                System.out.println("Loan created for the copy!");
            }
        }
        return true;
        }
        else
        {
            Loan loan=new Loan();
            loan.createLoan(id,borrowDate,period);
            loan.updateCopy(this);
            loan.updatePerson(person);
            loan.updateState(false);
            loans.add(loan);
            System.out.println("Loan created for the copy!");
        }
        return false;
    }
    
    public Boolean deleteLoan(int id)
    {
        Iterator<Loan> it = loans.iterator();
        while(it.hasNext()) 
        {
            Loan loan = it.next();
                if(loan.readID()==id)
                    return true;
               else
               {
                    loans.remove(loan);
                }
        }
        return false;
    }
   
   public int readSerialNumber()
   {
        return serialNumber;   
   }
   
   public void updateSerialNumber(int serialNumber)
   {
       this.serialNumber=serialNumber;   
   }
   
   public String readPurchaseDate()
   {
        return purchaseDate;    
   }
   
   public void updatePurchaseDate(String purchaseDate)
   {
        this.purchaseDate=purchaseDate;   
   }
   
   public int readPrice()
   {
       return price;
   }
   
   public void updatePrice(int price)
   {
       this.price=price;
   }
   
}
